<?php

/**
 * Created by PhpStorm.
 * User:money
 * Date: 17-6-22
 * Time: 10:00
 */

namespace  v1;
class OssCallback extends \ResourceClass{
    public $isActive = true;
    public $remark = 'OSS web 直传成功后回调地址，配置在 sdk/Config/Oss.php 中';

    public function post(array $req, array $post, &$result)
    {
        if (!isset($post['type'])) {
            return '文章类型不能为空';
        }

        $allowed_type_array = array('text','image','product','video','audio');
        if (!in_array($post['type'],$allowed_type_array)) {
            return '不被允许的类型';
        }
        switch ($post['type']) {
            case 'text':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_TEXT;
                break;
            case 'image':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_IMG;
                break;
            case 'product':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_PRODUCT;
                break;
            case 'video':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_VIDEO;
                break;
            case 'audio':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_AUDIO;
                break;
        }

        if ($type == \Orm\ArticleContent\ArticleContentClass::TYPE_PRODUCT) {// 商品图片回调
            try {
                if (!isset($post['product_id'])) {
                    return '商品id不能为空';
                }
                $product_img_class = new \Orm\ProductImg\ProductImgClass();
                $product_img_class['product_id'] = $post['product_id'];
                $product_img_class['url'] = \Util\Config::get('url_path', 'Oss').'/'.$post['filename'];
                $product_img_class['sort'] = 1;
                $product_img_class->insert();
                $product_img_class->loadById($product_img_class['id']);
                $result = array(
                    'id' => $product_img_class['id'],
                    'product_id' => $product_img_class['product_id'],
                    'url' => $product_img_class['url'],
                    'sort' => $product_img_class['sort'],
                );
            }catch (\Exception $e){}
        } else { // 文章素材回调
            if (!isset($post['article_id'])) {
                return '文章id不能为空';
            }
            try {
                $article_content_class = new \Orm\ArticleContent\ArticleContentClass();
                $article_content_class['article_id'] = $post['article_id'];
                $article_content_class['type'] = $type;
                $article_content_class['content'] = \Util\Config::get('url_path', 'Oss').'/'.$post['filename'];
//            $article_content_class['sort'] = $post['sort'];
                $article_content_class->insert();
                $article_content_class->loadById($article_content_class['id']);
                $result = array(
                    'id' => $article_content_class['id'],
                    'article_id' => $article_content_class['article_id'],
                    'type' => $type,
                    'content' => $article_content_class['content'],
                    'sort' => $article_content_class['sort'],
                );
            }catch (\Exception $e ) {
                return $e->getMessage();
            }
        }
        return '';
    }
}